﻿
using ICSharpCode.WinFormsUI.Docking;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevelopAssistant.Core.ToolBar
{
    public partial class SnippetBar : DockContent
    {
        public SnippetBar()
        {
            InitializeComponent();
        }

        private void SnippetBar_Load(object sender, EventArgs e)
        {
            LoadSnippetTreeData(null,string.Empty);
        }

        private void LoadSnippetTreeData(TreeNode owner, string pid)
        {
            using (var db = NORM.DataBase.DataBaseFactory.Default)
            {
                string sqlText = "SELECT [ID]" +
                    ",[NAME]" +
                    ",[Pid]" +
                    ",[Snippet]" +
                    ",[IsShow]" +
                    ",[OrderIndex]" +
                    ",[Childs]" +
                    " FROM [T_SnippetTree]" +
                    " WHERE [IsShow]='1'";

                if (!string.IsNullOrEmpty(pid))
                {
                    sqlText += " AND [Pid]='" + pid + "'";
                }
                else
                {
                    sqlText += " AND [Pid]='0'";
                }

                sqlText += " ORDER BY [OrderIndex] ASC";


                DataTable snippetDataTable = db.GetDataSet(CommandType.Text, sqlText,null).Tables[0];

                foreach (DataRow dataRow in snippetDataTable.Rows)
                {
                    string cid = dataRow["ID"] + "";
                    string childs = dataRow["Childs"] + "";

                    TreeNode node = new TreeNode();
                    node.Text = dataRow["NAME"] + "";
                    node.Tag = dataRow["Snippet"] + "";                   
                   
                    if (Int32.Parse(childs)>0)
                    {
                        node.ImageIndex = 0;
                        node.SelectedImageIndex = 0;
                    }
                    else
                    {
                        node.ImageIndex = 2;
                        node.SelectedImageIndex = 2;
                    }

                    if (owner == null && string.IsNullOrEmpty(pid))
                        SnippetTree.Nodes.Add(node);

                    if (owner != null)
                    {                        
                        owner.Nodes.Add(node);
                        owner.ImageIndex = 1;
                        owner.SelectedImageIndex = 1;
                        owner.Expand();
                    }

                    LoadSnippetTreeData(node, cid);
                }

            }
        }

        private void SnippetTree_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            e.Node.ImageIndex = 0;
            e.Node.SelectedImageIndex = e.Node.ImageIndex;
        }

        private void SnippetTree_AfterExpand(object sender, TreeViewEventArgs e)
        {
            e.Node.ImageIndex = 1;
            e.Node.SelectedImageIndex = e.Node.ImageIndex;
        }        
    }
}
